<template>
  <div class="serviceLike">
    <div class="likeleft">
      <div class="likecenter">
        <div class="title">偏好</div>
        <div class="baseconfig">基础配置</div>
      </div>
    </div>
    <div class="likeright">
      <div class="area">
        <div>家庭面积：</div>
        <div
          class="areaButtons"
          v-for="(item, index) in ruleForm.systemConfigHobbies.areaHouse"
          :key="index"
        >
          {{ item.areaScopeName }}
        </div>
      </div>
      <div class="homePeople">
        <div>家庭人口：</div>
        <div class="group">
          <div
            class="homePeopleItem"
            v-for="(item, index) in ruleForm.systemConfigHobbies.populations"
            :key="index"
          >
            <div>
              <el-input
                v-model="
                  ruleForm.systemConfigHobbies.populations[index]
                    .populationValue
                "
                placeholder="请输入内容"
              ></el-input>
            </div>
            <el-button
              icon="el-icon-plus"
              style="background-color:#fd804c;color: #fff;width:30px;height:30px;margin-left:5px;display: flex;
                justify-content: center;align-items: center;"
              v-if="index === 0"
              @click="addPeople"
            >
            </el-button>
            <el-button
              icon="el-icon-minus"
              style="background-color:#fd804c;color: #fff;width:30px;height:30px;margin-left:5px;display: flex;
                justify-content: center;align-items: center;"
              v-if="index != 0"
              @click="deletePeople(index)"
            >
            </el-button>
          </div>
        </div>
      </div>
      <div class="tasteLike">
        <div>口味偏好：</div>
        <div class="group">
          <div
            class="tasteItem"
            v-for="(item, index) in ruleForm.systemConfigHobbies.foods"
            :key="index"
          >
            <div>
              <el-input
                v-model="ruleForm.systemConfigHobbies.foods[index].foodValue"
                placeholder="请输入内容"
              ></el-input>
            </div>
            <el-button
              icon="el-icon-plus"
              style="background-color:#fd804c;color: #fff;width:30px;height:30px;margin-left:5px;display: flex;
                justify-content: center;align-items: center;"
              v-if="index === 0"
              @click="addTaste"
            >
            </el-button>
            <el-button
              icon="el-icon-minus"
              style="background-color:#fd804c;color: #fff;width:30px;height:30px;margin-left:5px;display: flex;
                justify-content: center;align-items: center;"
              v-if="index != 0"
              @click="deleteTaste(index)"
            >
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <div class="likerightbehind">
      <div class="save" @click="save">保存</div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["systemConfigHobbies"],
  data() {
    return {
      ruleForm: {
        systemConfigHobbies: {
          areaHouse: [],
          foods: [
            {
              configType: "system_config_hobbies",
              hobbyType: "food",
              foodValue: ""
            }
          ],
          populations: [
            {
              configType: "system_config_hobbies",
              hobbyType: "population",
              populationValue: ""
            }
          ]
        }
      }
    };
  },
  methods: {
    addPeople() {
      this.ruleForm.systemConfigHobbies.populations.push({
        configType: "system_config_hobbies",
        hobbyType: "population",
        populationValue: ""
      });
    },
    deletePeople(index) {
      this.ruleForm.systemConfigHobbies.populations.splice(index, 1);
    },
    addTaste() {
      this.ruleForm.systemConfigHobbies.foods.push({
        configType: "system_config_hobbies",
        hobbyType: "food",
        foodValue: ""
      });
    },
    deleteTaste(index) {
      this.ruleForm.systemConfigHobbies.foods.splice(index, 1);
    },
    // 保存接口
    async save() {
      const flag = this.validate();
      if (!flag) {
        // this.$message.error('请将偏好里面的数据填写完整')
        return false;
      }
      const data = {
        systemConfigHobbies: {
          foods: this.ruleForm.systemConfigHobbies.foods,
          populations: this.ruleForm.systemConfigHobbies.populations
        }
      };
      this.$http({
        url: "/hsssystemadmin/config/system/updateSystemConfig",
        method: "post",
        data
      })
        .then(res => {
          this.$message.success("偏好基础配置成功");
          this.$emit("research");
        })
        .catch(err => {
          this.$message.error("系统繁忙，请稍后重试");
        });
    },
    // 校验数据
    validate() {
      let flag = true;
      let populationValueList = [];
      let foodsValueList = [];
      try {
        this.ruleForm.systemConfigHobbies.populations.forEach(item => {
          // 非空校验
          if (item.populationValue == "") {
            flag = false;
            this.$message.error("请将偏好里面的数据填写完整");
            throw new Error("数据为空");
          }
        });
      } catch (err) {
        console.log(err);
      }

      try {
        this.ruleForm.systemConfigHobbies.foods.forEach(item => {
          // 非空校验
          if (item.foodValue == "") {
            flag = false;
            this.$message.error("请将偏好里面的数据填写完整");
            throw new Error("数据为空");
          }
        });
      } catch (err) {
        console.log(err);
      }
      // 人数重复校验
      this.ruleForm.systemConfigHobbies.populations.forEach(item => {
        populationValueList.push(item.populationValue);
      });
      const newPopArray = Array.from(new Set(populationValueList));
      if (
        newPopArray.length <
        this.ruleForm.systemConfigHobbies.populations.length
      ) {
        flag = false;
        this.$message.error("人数填写不能前后重复");
      }
      // 口味重复校验
      this.ruleForm.systemConfigHobbies.foods.forEach(item => {
        foodsValueList.push(item.foodValue);
      });
      const newFoodArray = Array.from(new Set(foodsValueList));
      if (
        newFoodArray.length < this.ruleForm.systemConfigHobbies.foods.length
      ) {
        flag = false;
        this.$message.error("口味偏好填写不能前后重复");
      }
      return flag;
    }
  },
  watch: {
    systemConfigHobbies(newvalue) {
      if (newvalue) {
        // 房屋面积
        this.ruleForm.systemConfigHobbies.areaHouse = newvalue.areaHouse;
        // 口味枚举
        if (newvalue.foods.length > 0) {
          this.ruleForm.systemConfigHobbies.foods = newvalue.foods;
        }
        // 人数配置枚举
        if (newvalue.populations.length > 0) {
          this.ruleForm.systemConfigHobbies.populations = newvalue.populations;
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.serviceLike {
  display: flex;
  justify-content: space-between;
  .likeleft {
    width: 150px;
    min-height: 150px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    .likecenter {
      width: 100px;
      height: 70px;
      .title {
        width: 100%;
        height: 35px;
        background-color: #f56a23;
        font-size: 22px;
        color: #fff;
        line-height: 35px;
        text-align: center;
      }
      .baseconfig {
        width: 100%;
        height: 35px;
        font-size: 22px;
        line-height: 35px;
        text-align: center;
        color: #aaa;
      }
    }
  }
  .likerightbehind {
    width: 150px;
    min-height: 150px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    .save {
      width: 70px;
      height: 40px;
      font-size: 22px;
      color: #fff;
      background-color: #00cb75;
      text-align: center;
      line-height: 40px;
      cursor: pointer;
    }
  }
  .likeright {
    background-color: #fff;
    min-height: 150px;
    margin-left: 10px;
    margin-right: 10px;
    padding: 20px;
    box-sizing: border-box;
    flex: 1;
    .area {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      font-size: 12px;
      height: 40px;
      margin-bottom: 10px;
      .areaButtons {
        height: 24px;
        line-height: 24px;
        text-align: center;
        border: 1px solid #dcdfe6;
        border-radius: 4px;
        padding: 0 10px;
        margin-right: 15px;
      }
    }
    .homePeople {
      font-size: 12px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .group {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        max-width: 600px;
        flex-wrap: wrap;
        .homePeopleItem {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-right: 10px;
          margin-bottom: 5px;
          min-width: 110px;
          /deep/ .el-input__inner {
            padding: 0 5px;
            height: 30px;
            width: 84px;
          }
        }
      }
    }
    .tasteLike {
      font-size: 12px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .group {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        max-width: 600px;
        flex-wrap: wrap;
        .tasteItem {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-right: 10px;
          margin-bottom: 5px;
          min-width: 110px;
          /deep/ .el-input__inner {
            padding: 0 5px;
            height: 30px;
            width: 84px;
          }
        }
      }
    }
  }
}
</style>
